# -*- coding: utf-8 -*-
# @Time    : 2024/8/29 13:54
# @Author  : Alvin
# @File    : token.py
from datetime import datetime, timedelta

from authlib.jose import jwt
from flask import current_app


def generate_auth_token(uid, ac_type, scope=None):
    expiration = current_app.config.get('TOKEN_EXPIRATION', 2 * 60 * 60)
    header = {'alg': 'HS256'}
    secret_key = current_app.config['SECRET_KEY']
    payload = dict(
        uid=uid,
        type=ac_type,
        scope=scope,
        exp=datetime.utcnow() + timedelta(seconds=expiration)
    )
    token = jwt.encode(header, payload, secret_key)
    return token
